package com.attack.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.attack.manager.OrderDetailManager;
import com.attack.model.OrderDetailModel;
import com.common.component.Pagination;
import com.common.utils.DateUtil;
import com.framework.model.IData;
import com.framework.model.impl.DataMap;
import com.framework.web.BaseController;

@Controller
@RequestMapping(value = "/management/order/detail")
public class OrderDetailController extends BaseController{

	 private static String modelUrl="management/order/detail/";
	 
    @Autowired
	private OrderDetailManager orderDetailManager;
    
    
    /**
     * 功能：页面分页列表
     * @param vo
     * @param param
     * @return
     */
	@RequestMapping("/pageList")
	public ModelAndView pageList(@ModelAttribute("page") Pagination vo,@ModelAttribute("orderDetailModel") OrderDetailModel param) {
		ModelAndView modelView = new ModelAndView(modelUrl+"pagelist");
		String startIncomeDate="";
		String endIncomeDate="";
		if(StringUtils.isEmpty(param.getStartIncomeDate())){
			startIncomeDate = DateUtil.getCurrentDate();
			param.setStartIncomeDate(startIncomeDate);
		}else{
			startIncomeDate = param.getStartIncomeDate();
		}
		if(StringUtils.isEmpty(param.getEndIncomeDate())){
			endIncomeDate = DateUtil.getReportDays(1);
			param.setEndIncomeDate(endIncomeDate);
		}else{
			endIncomeDate = param.getEndIncomeDate();
		}
		
		List<DataMap> dataList = orderDetailManager.getPageObjectList(vo, param);
		modelView.addObject("vo", vo);
		modelView.addObject("param", param);
		modelView.addObject("startIncomeDate", startIncomeDate);
		modelView.addObject("endIncomeDate", endIncomeDate);
		modelView.addObject("objList", dataList);
		
		//获取到期总金额
		IData sumData = (DataMap)orderDetailManager.getObjectInfo("getDaySumIncome", param);
		if(sumData!=null){
			modelView.addObject("totalMoney", sumData.get("monthIncome"));
		}else{
			modelView.addObject("totalMoney",0);
		}
		
		return modelView;
	}
	
	
    /**
     * 功能：页面分页列表
     * @param vo
     * @param param
     * @return
     */
	@RequestMapping("/getObjectList")
	public ModelAndView getObjectList(@ModelAttribute("orderDetailModel") OrderDetailModel param) {
		ModelAndView modelView = new ModelAndView(modelUrl+"list");
		List<DataMap> dataList = orderDetailManager.getObjectList(param);
		modelView.addObject("param", param);
		modelView.addObject("objList", dataList);
		return modelView;
	}
	
	
	/**
	 * 功能：初始化添加页面
	 * @param orderDetailModel
	 * @return
	 */
	@RequestMapping("initAdd")
	public ModelAndView initAdd(@ModelAttribute("orderDetailModel") OrderDetailModel orderDetailModel) {
		ModelAndView modelView = new ModelAndView(modelUrl+"add");
		log.info("add is page");
		modelView.addObject("vo",orderDetailModel);
		return modelView;
	}
	
	
	/**
	 * 功能：初始化编辑页面
	 * @param intID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/{intID}/initEdit")
	public ModelAndView initEdit(@PathVariable("intID") String intID) {
		ModelAndView modelView = new ModelAndView(modelUrl+"update");
		IData dataMap = new DataMap();
		dataMap.put("intID", intID);
		OrderDetailModel orderDetailModel = (OrderDetailModel)orderDetailManager.getObjectInfo(dataMap);
		modelView.addObject("vo",orderDetailModel);
		return modelView;
	}
	
	
	/**
	 * 功能：根据客户ID查询对应的订单信息
	 * @param intID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/showList")
	public ModelAndView showList(@ModelAttribute("orderDetailModel") OrderDetailModel param) {
		ModelAndView modelView = new ModelAndView(modelUrl+"showList");
		List<DataMap> orderList = orderDetailManager.getObjectList(param);
		modelView.addObject("orderDetailList",orderList);
		return modelView;
	}
	
	/**
	 * 功能：执行插入操作
	 * @param orderDetailModel
	 * @return
	 */
	@ResponseBody
	@RequestMapping("add")
	public Object insert(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("orderDetailModel") OrderDetailModel orderDetailModel) {
		try{
			int opNum = orderDetailManager.insertObjectAndGetID(orderDetailModel);
			if(opNum>0){
				return super.jsonReturn(response,200);
			}else{
				return super.jsonReturn(response,300);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage());
		}
	}
	
	/**
	 * 功能：执行更新操作
	 * @param orderDetailModel
	 * @return
	 */
	@ResponseBody
	@RequestMapping("update")
	public Object update(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("orderDetailModel") OrderDetailModel orderDetailModel) {
		try{
			orderDetailModel.setOperator(getSessionUserInfo().getStaffName());
			int opNum =orderDetailManager.updateObject("updateStatusObject",orderDetailModel);
			if(opNum>0){
				return super.jsonReturn(response,200,false);
			}else{
				return super.jsonReturn(response,300,false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage(),false);
		}
	}
	
	/**
	 * 功能：执行单记录删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/{intID}/delete")
	public Object delete(HttpServletResponse response,@PathVariable("intID") int intID) {
		try{
			IData dataMap = new DataMap();
			dataMap.put("intID", intID);
			int opNum =orderDetailManager.deleteObject(dataMap);
			if(opNum>0){
				return super.jsonReturn(response,200,false);
			}else{
				return super.jsonReturn(response,300,false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage(),false);
		}
	}
	
	/**
	 * 功能：执行批量删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteAll")
	public Object deleteAll(HttpServletResponse response,HttpServletRequest request) {
		String ids = request.getParameter("ids");
		try{
			IData dataMap = new DataMap();
			dataMap.put("ids", ids);
			int opNum =orderDetailManager.deleteObject("deleteAllObject",dataMap);
			if(opNum>0){
				return super.jsonReturn(response,200,false);
			}else{
				return super.jsonReturn(response,300,false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage(),false);
		}
	}
	
}